package cn.org.timespace.ons.socket.mina.filter.command.concrete;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.mina.core.session.IoSession;

import cn.org.timespace.ons.socket.mina.filter.ESCardProtocalPack;
import cn.org.timespace.ons.socket.mina.filter.command.AbstractESCardCommand;
import cn.org.timespace.ons.socket.mina.util.ProcessPackage;
import cn.org.timespace.ons.vo.base.Machines;

/**
 * 信息终端取配置信息及数据交换地址
 * @author wind
 *
 */
public class MachinesCommand extends AbstractESCardCommand {
	private final static Log log = LogFactory.getLog(MachinesCommand.class);
	private final static int numSta = 0;//信息机配置信息内容长度
	
	@Override
	public void processPackage(IoSession session, ESCardProtocalPack pack) {
		ESCardProtocalPack packReplay = ProcessPackage.getPackReplay(pack);
		if(checkPackage(pack, ESCardProtocalPack.SYNC_FRAME_NAME_UP)) {//消息正确
			//解析信息机配置信息
			String ms = analyticalMsg(pack);
			if(ms != null && !ms.equals("")){
				packReplay.setPackageContents(ms);
				packReplay.setCheckCodeAndPackageLength();
				log.debug("回复：" + packReplay.toString());

				session.write(packReplay.toString());
				try {
					Thread.sleep(3000);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				session.close(false);
				return;
			}
			
		}
			//出错
			packReplay.setPackageContents("ERROR");
			packReplay.setCheckCodeAndPackageLength();
			log.debug(packReplay.toString());
			session.write(packReplay.toString());
			try {
				Thread.sleep(3000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			session.close(false);
		
	}
	
	/**
	 * 	数据交换状态信息
	 * @param pack
	 * @return
	 */
	public String analyticalMsg(ESCardProtocalPack pack) {
		String result = "";
		String packageContents = pack.getPackageContents();//包内容
		try{
			int contentLength = packageContents.length();
			if(contentLength < numSta) {//如果包内容长度不是numSta
				log.debug("包内容长度不是numSta,解析失败");
				return null;
			}
			
			//信息终端的组合ID
			String machinesGroupID = pack.getMachineNo();//信息终端的ID
			Machines m = ProcessPackage.getMachinesFId(machinesGroupID);
			if(m != null){
				// 1.取信息终端配置
				String conf = ProcessPackage.getMachinesConf(machinesGroupID);
				
				// 2.取信息终端申请的平台
				String apples = ProcessPackage.getMSInfo(machinesGroupID);
				// 3.取数据交换的地址
				String momAddrs = ProcessPackage.getMomAddr(machinesGroupID,apples);
				if(momAddrs != null && !momAddrs.equals("")){
					String momAddr[] = momAddrs.split(";");
					String momHexIPAddress = momAddr[1];//组装之后的16进制数据交换地址
					if(momHexIPAddress.length() >0 ){
						// 5.将信息终端和数据交换中心地址写入信息终端分配表machinesMom
						ProcessPackage.addMachinesMom(machinesGroupID, momAddr[0]);
						// 6.返回信息终端配置及数据交换地址到信息终端
						result = momHexIPAddress+conf;//配置信息和数据交换地址
					}
				}
			}
			return result;
		}
		catch(Exception e) {
			e.printStackTrace();
			log.error("解析失败");
			log.debug(e.toString());
		}
		return null;
	}
	
}
